package com.yonyou.pmclouds.completedcatalog.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.entity.PageObject;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.exception.IllegalRequestArgException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.completedcatalog.entity.CompletedCatalogBVO;
import com.yonyou.pmclouds.completedcatalog.entity.CompletedCatalogVO;
import com.yonyou.pmclouds.completedcatalog.rmiitf.CompletedCatalogMaintain;
import com.yonyou.pmclouds.completedcatalog.rmiitf.CompletedCatalogQuery;
import com.yonyou.pmclouds.outter.user.UserInfoQuery;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 竣工资料目录
 *
 * @author yzx
 */
@RestController
@RequestMapping("/completedCatalog")
public class CompletedCatalogController {
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private CompletedCatalogMaintain maintain;
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private CompletedCatalogQuery query;
    @Autowired
    private UserInfoQuery userQuery;

    private static final String[] USER_ID_FIELDS = {CompletedCatalogVO.CREATOR};
    private static final String[] USER_NAME_FIELDS = {CompletedCatalogVO.CREATOR_NAME};

    /**
     * 获取所有符合要求的竣工资料目录数据
     *
     * @param
     * @return java.util.List<com.yonyou.pmclouds.completedcatalog.entity.CompletedCatalogVO>
     * @author yzx
     * @date 2018/5/24 11:00
     */
    @GetMapping("/list")
    public List<CompletedCatalogVO> list() throws BusinessException {
        List<CompletedCatalogVO> resultVOs = query.queryBypkTenantId(RuntimeEnvironment.getTenantId());
        // 为创建人赋值
        userQuery.tranUserInfoField(resultVOs.toArray(new CompletedCatalogVO[resultVOs.size()]), USER_ID_FIELDS, USER_NAME_FIELDS);
        return resultVOs;
    }

    /**
     * 根据条件查询
     *
     * @param condition 可以用空格分割成多个条件
     * @param start     开始的页码
     * @param size      每页条数
     * @return
     * @throws BusinessException
     */
    @RequestMapping(value = "/search")
    public PageObject search(@RequestParam(required = false) String condition,
                             @RequestParam(required = false) Integer start,
                             @RequestParam(required = false) Integer size) throws BusinessException {
        PageObject pageObject = query.search(condition, start, size);
        // 为创建人赋值
        userQuery.tranUserInfoField(pageObject.getObjects(), USER_ID_FIELDS, USER_NAME_FIELDS);
        return pageObject;
    }

    @PostMapping("/insertOrUpdate")
    public CompletedCatalogVO insertOrUpdate(@RequestBody @Validated CompletedCatalogVO catalogVO, BindingResult errors) throws BusinessException {
        if (errors.hasErrors()) {
            throw new IllegalRequestArgException();
        }
        catalogVO.setPkTenant(RuntimeEnvironment.getTenantId());
        // 主键为空表示新增
        String id;
        if (StringUtils.isEmpty(catalogVO.getPkCompletedCatalog())) {
            id = maintain.insert(catalogVO);
        } else {
            id = maintain.update(catalogVO);
        }
        CompletedCatalogVO resultVO = query.getById(id);
        // 为创建人赋值
        userQuery.tranUserInfoField(new CompletedCatalogVO[]{resultVO}, USER_ID_FIELDS, USER_NAME_FIELDS);
        return resultVO;
    }

    @GetMapping("/queryOne")
    public CompletedCatalogVO queryOne(String pkCompletedCatalog) throws BusinessException {
        CompletedCatalogVO resultVO = query.getById(pkCompletedCatalog);
        // 为创建人赋值
        userQuery.tranUserInfoField(new CompletedCatalogVO[]{resultVO}, USER_ID_FIELDS, USER_NAME_FIELDS);
        return resultVO;
    }

    @PostMapping("/deleteCatalog")
    public void deleteCatalog(@RequestBody CompletedCatalogVO catalogVO) throws BusinessException {
        if (StringUtils.isEmpty(catalogVO.getPkCompletedCatalog()) || StringUtils.isEmpty(catalogVO.getChangeFlag())) {
            throw new IllegalRequestArgException();
        }
        maintain.deleteCatalog(catalogVO);
    }

    @PostMapping("/deleteCatalogDetail")
    public void deleteCatalogDetail(@RequestBody CompletedCatalogBVO catalogBVO) throws BusinessException {
        if (StringUtils.isEmpty(catalogBVO.getPkCompletedCatalogB()) || StringUtils.isEmpty(catalogBVO.getChangeFlag())) {
            throw new IllegalRequestArgException();
        }
        maintain.deleteCatalogDetail(catalogBVO);
    }

    /**
     * 确定当前目录是否被引用
     *
     * @param pkCompletedCatalog
     * @return boolean
     * @author yzx
     * @date 2018/6/25 11:14
     */
    @GetMapping("/isExistRef")
    public boolean isExistRef(@RequestParam String pkCompletedCatalog) throws BusinessException {
        return query.isExistRef(pkCompletedCatalog);
    }

    /**
     * 复制目录接口
     *
     * @param pkCompletedCatalog, copyName
     * @return void
     * @author yzx
     * @date 2018/6/27 9:26
     */
    @RequestMapping("/copy")
    public void copy(@RequestParam String pkCompletedCatalog, @RequestParam String copyName) throws BusinessException {
        CompletedCatalogVO catalogVO = query.getById(pkCompletedCatalog);
        if (catalogVO == null) {
            throw new BusinessException("目录已经不存在!");
        }
        catalogVO.setSname(copyName);
        maintain.insert(catalogVO);
    }
}
